//
//  XZCoursePackageCell.m
//  Teecloud
//
//  Created by PC on 2018/10/29.
//  Copyright © 2018年 小志. All rights reserved.
//

#import "XZCoursePackageCell.h"
#import "GMMyclassModel.h"
#import "ZJTimeCountDown.h"
@interface XZCoursePackageCell ()

@property (nonatomic, strong) ZJTimeCountDown *countDown;
//名称
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
//类型
@property (weak, nonatomic) IBOutlet UILabel *resTypeNameLabel;
//来源
@property (weak, nonatomic) IBOutlet UILabel *frontNameLabel;
//状态
@property (weak, nonatomic) IBOutlet UILabel *statusLabel;


@end
//通过
#define KPASSColor [UIColor colorWithHexString:@"#009900" andAlpha:1.0]
//未通过
#define KUNPASSColor [UIColor colorWithHexString:@"#FF0000" andAlpha:1.0]
//培训中
#define KTrainColor [UIColor colorWithHexString:@"#FF9900" andAlpha:1.0]
@implementation XZCoursePackageCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    self.resTypeNameLabel.layer.cornerRadius = 2.0f;
    self.resTypeNameLabel.clipsToBounds = YES;
    self.resTypeNameLabel.layer.borderWidth=0.5f;
    self.resTypeNameLabel.layer.borderColor = [UIColor colorWithHexString:@"#FF8700" andAlpha:1.0f].CGColor;

}

- (void)setModel:(GMMyclassModel *)model{
    _model = model;
    self.titleLabel.text = model.Name;
    self.resTypeNameLabel.text = model.ResTypeName;
    self.frontNameLabel.text =model.FromName;

    
    if ([model.ResTypeName isEqualToString:@"视频"]) {
        NSString *str = @"%";
        NSString *statusStr = [NSString stringWithFormat:@"%@分钟 %@%@",model.TimeLen,model.Percent,str];
        [self attributedString:statusStr scoreString:[NSString stringWithFormat:@"%@",model.Percent] label:self.statusLabel];
    }else if ([model.ResTypeName isEqualToString:@"习题"] ||[model.ResTypeName isEqualToString:@"题集"] ){
        NSString *str = @"%";
        NSString *statusStr = [NSString stringWithFormat:@"%@题 %@%@",model.ExerCount,model.Percent,str];;
        [self attributedString:statusStr scoreString:[NSString stringWithFormat:@"%@",model.Percent] label:self.statusLabel];
    }else if ([model.ResTypeName isEqualToString:@"试卷"]){
        NSString *string = [NSString stringWithFormat:@"测试%@次 平均%@分",model.Count,model.Score];
        NSString *num = [NSString stringWithFormat:@"%@",model.Count];
        NSString *score = [NSString stringWithFormat:@"%@",model.Score];
        NSMutableAttributedString *originalAttributedString = [[NSMutableAttributedString alloc] initWithString:string];
        [originalAttributedString addAttribute:NSForegroundColorAttributeName value:KTrainColor range:NSMakeRange(string.length-(score.length +1), score.length)];
        [originalAttributedString addAttribute:NSForegroundColorAttributeName value:KTrainColor range:NSMakeRange(2, num.length)];
        self.statusLabel.attributedText = originalAttributedString;
    }else if ([model.ResTypeName isEqualToString:@"考试"]){
        [self isExam:model];
    }else if ([model.ResTypeName isEqualToString:@"文档"]){
        NSString *str = @"%";
        NSString *statusStr = [NSString stringWithFormat:@"%@页 %@%@",model.PageCount,model.Percent,str];
        [self attributedString:statusStr scoreString:[NSString stringWithFormat:@"%@",model.Percent] label:self.statusLabel];
    }else if ([model.ResTypeName isEqualToString:@"课程包"]|| [model.ResTypeName isEqualToString:@"资源包"]){
        NSString *pageCount = [NSString stringWithFormat:@"%@",model.PageCount];
        NSString *string =  [NSString stringWithFormat:@"%@资源",model.PageCount];
        NSMutableAttributedString *originalAttributedString = [[NSMutableAttributedString alloc] initWithString:string];
        [originalAttributedString addAttribute:NSForegroundColorAttributeName value:KTrainColor range:NSMakeRange(0, pageCount.length)];
        self.statusLabel.attributedText = originalAttributedString;
    }
}
//model.ResTypeName=@“考试”
- (void)isExam:(GMMyclassModel *)model{
    if ([model.TestStatus integerValue] == 0) {
        NSInteger second = model.SpaceTime;
        if (second > 0) {
            [self.countDown zj_timeCountDownWithSecondTime:second completeBlock:^(NSInteger hour, NSInteger minute, NSInteger second) {
                self.statusLabel.text = [NSString stringWithFormat:@"距离开考 %02ld:%02ld:%02ld",(long)hour,(long)minute,(long)second];
            }];
        }else{
            self.statusLabel.text = @"";
        }
        
    }else if ([model.TestStatus integerValue] == 1){
        self.statusLabel.textColor =[UIColor colorWithHexString:@"#0065EA" andAlpha:1.0f];
        self.statusLabel.text = @"进入考试";
    }else if ([model.TestStatus integerValue] == 2){
        self.statusLabel.textColor =[UIColor colorWithHexString:@"#0065EA" andAlpha:1.0f];
        self.statusLabel.text = @"继续考试";
    }else if ([model.TestStatus integerValue] == 3){
        self.statusLabel.textColor = kRedColor;
        self.statusLabel.text = @"停止入场";
    }else if ([model.TestStatus integerValue] == 4){
        if (model.PassMode == -1) { //缺考
            self.statusLabel.text = @"缺考";
            self.statusLabel.textColor = kRedColor;
        }else if (model.PassMode  == 0){//未通过
            NSString *passString = [NSString stringWithFormat:@"%@分(未通过)",model.Score];
            NSString *scoreStr = [NSString stringWithFormat:@"%@",model.Score];
            [self textAttributedString:passString scoreString:scoreStr label:self.statusLabel];
        }else if (model.PassMode  == 1){//通过
            NSString *passString = [NSString stringWithFormat:@"%@分(通过)",model.Score];
            NSString *scoreStr = [NSString stringWithFormat:@"%@",model.Score];
            [self textAttributedString:passString scoreString:scoreStr label:self.statusLabel];
        }
        
    }else if ([model.TestStatus integerValue] == 9){
        self.statusLabel.text = @"考试结束";
        self.statusLabel.textColor = kRedColor;
    }
}

- (void)attributedString:(NSString *)string scoreString:(NSString *)scoreString label:(UILabel *)label{
    NSMutableAttributedString *originalAttributedString = [[NSMutableAttributedString alloc] initWithString:string];
//    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init];
//    [originalAttributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, string.length)];
    
    [originalAttributedString addAttribute:NSForegroundColorAttributeName value:KTrainColor range:NSMakeRange(string.length-scoreString.length-1, scoreString.length+1)];
    label.attributedText = originalAttributedString;
}

- (void)textAttributedString:(NSString *)string scoreString:(NSString *)scoreString label:(UILabel *)label{
    
    NSMutableAttributedString *originalAttributedString = [[NSMutableAttributedString alloc] initWithString:string];
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init];
    [originalAttributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, string.length)];
    
    [originalAttributedString addAttribute:NSForegroundColorAttributeName value:KTrainColor range:NSMakeRange(0, scoreString.length)];
    
    //字符串末尾有(未通过)
    if ([string hasSuffix:@"(未通过)"]) {
        [originalAttributedString addAttribute:NSForegroundColorAttributeName value:KUNPASSColor range:NSMakeRange(string.length-5, 5)];
        
    } else {
        [originalAttributedString addAttribute:NSForegroundColorAttributeName value:KPASSColor range:NSMakeRange(string.length-4, 4)];
    }
    label.attributedText = originalAttributedString;
}
#pragma mark - Setter
- (ZJTimeCountDown *)countDown{
    if (!_countDown) {
        _countDown = [[ZJTimeCountDown alloc]init];
    }
    return _countDown;
}
//销毁定时器
- (void)dealloc{
    [self.countDown zj_timeDestoryTimer];
}
@end
